package com.rsy.crm.workbench.service.Impl;

import com.rsy.crm.settings.dao.UserDao;
import com.rsy.crm.settings.domain.User;
import com.rsy.crm.util.SqlSessionUtil;

import com.rsy.crm.vo.PageVo;
import com.rsy.crm.workbench.dao.ActivityDao;
import com.rsy.crm.workbench.dao.ActivityRemarkDao;
import com.rsy.crm.workbench.domain.Activity;
import com.rsy.crm.workbench.domain.ActivityRemark;
import com.rsy.crm.workbench.service.ActivityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 市场业务层
 */
public class ActivityServiceImpl implements ActivityService {

    private  ActivityDao activityDao =  SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private ActivityRemarkDao activityRemarkDao =  SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);
    /**
     * 添加活动
     * @param activity
     * @return
     */
    @Override
    public boolean save(Activity activity) {
        boolean flag = true;
        int count = activityDao.save(activity);

        if (count != 1){
            flag = false;
        }

        return flag;
    }

    /**
     * 活动分页
     * @param map
     * @return
     */
    @Override
    public PageVo<Activity> pageList(Map<String, Object> map) {

        //获取数据
        List<Activity> list = activityDao.getActivityListByCondition(map);
        //获取总记录数
        int total = activityDao.getTotalByCondition(map);

        PageVo<Activity> vo = new PageVo<>();
        vo.setList(list);
        vo.setTotal(total);

        return vo;
    }

    /**
     * 删除活动
     * @param ids
     * @return
     */
    @Override
    public boolean deleteAct(String[] ids) {
        boolean flag = true;

        //查询数需要删除的备注数量
        int countr = activityRemarkDao.getCountByAids(ids);
        //删除备注，返回删除的个数
        int countrd = activityRemarkDao.deleteByAids(ids);
        if(countrd != countr){
            flag = false;
        }
        //删除市场活动
        int countd = activityDao.delete(ids);
        if (countd != ids.length){
            flag = false;
        }
        return flag;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getUserListAndActivity(String id) {

        List<User> list = userDao.getUser();
        Activity a = activityDao.getById(id);

        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("a",a);

        return map;
    }

    /**
     * 更新
     * @param a
     * @return
     */
    @Override
    public boolean update(Activity a) {
        boolean flag = true;

        int count = activityDao.update(a);
        if (count != 1){
            flag = false;
        }
        return flag;
    }

    @Override
    public Activity detail(String id) {

        Activity a = activityDao.detail(id);


        return a;
    }

    /**
     * 市场活动详细信息备注展示
     * @param id
     * @return
     */
    @Override
    public List<ActivityRemark> getRemarkListByAid(String id) {

        List<ActivityRemark> list = activityRemarkDao.getRemarkListByAid(id);

        return list;
    }

    /**
     * 删除备注
     * @param id
     * @return
     */
    @Override
    public boolean deleteRemark(String id) {
        boolean flag = true;
        int count = activityRemarkDao.deleteRemark(id);
        if (count!=1){
            flag=false;
        }

        return flag;
    }
    /**
     * 添加备注
     * @param ar
     * @return
     */
    @Override
    public boolean saveRemark(ActivityRemark ar) {
        boolean flag = true;
        int count = activityRemarkDao.saveRemark(ar);
        if (count!=1){
            flag=false;
        }
        return flag;

    }
    /**
     * 更新备注
     * @param ar
     * @return
     */
    @Override
    public boolean updateRemark(ActivityRemark ar) {
        boolean flag = true;
        int count = activityRemarkDao.updateRemark(ar);
        if (count != 1){
            flag=false;
        }
        return flag;
    }

    /**
     * 线索 与 活动关联
     * @param clueId
     * @return
     */
    @Override
    public List<Activity> getActivityListByClueId(String clueId) {

        List<Activity> list = activityDao.getActivityListByClueId(clueId);

        return list;
    }

    /**
     * 查询市场活动列表（根据名称模糊查+排除掉已经关联指定线索的列表
     * @param map
     * @return
     */
    @Override
    public List<Activity> getActivityListByNameAndNotByClueId(Map<String, String> map) {

        List<Activity> aList = activityDao.getActivityListByNameAndNotByClueId(map);

        return aList;
    }

    /**
     * 查询市场活动列表（根据名称模糊查）
     * @param aname
     * @return
     */
    @Override
    public List<Activity> getActivityListByName(String aname) {
        List<Activity> list = activityDao.getActivityListByName(aname);

        return list;
    }

}
